<template>
	<view class="home">

		<view class="top" style="height: 80px;">
			<!-- v-model="value"双向绑定将value值传回去实现点击标签实现搜索 -->
			<navbarIndex :isSearch="true" @input="change" v-model="value"></navbarIndex>
		</view>
		<view class="home-list">
			<!-- isHistory为true时显示搜索历史 -->
			<view class="labelBox" v-if="isHistory">
				<view class="labelHeader">
					<text class="labelTitle">历史搜索</text>
					<text class="labelClear" @click="clear">清空</text>
				</view>
				<view class="labelCont" v-if="historyLists.length > 0">
					<view class="labelContItem" v-for="(item,index) in historyLists" :key="index" @click="clickHistory(item)">
						{{item.name}}
					</view>
				</view>
				<view v-else class="withoutData">
					—————— 没有历史记录 ——————
				</view>
			</view>
			<!-- isHistory为false时显示搜索结果 -->
			<view v-else class="result">
				<uni-load-more status="loading" iconType="snow" v-if="isLoading"></uni-load-more>
				<list-scroll class="list-scroll" @loadmore="loadmore">
					<view v-if="searchList.length>0">
						<list-card :item="item" v-for="(item,index) in searchList" :key="index" @click="setHistory"></list-card>
					</view>
					<view v-if="searchList.length===0&&!isLoading" class="withoutData">
						—————— 暂无相关记录 ——————
					</view>
					<!-- @click="setHistory"子组件把数据发给父组件的 -->
				</list-scroll>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex"
	export default {
		data() {
			return {
				// 标志出是搜索字段还是搜索结果
				isHistory: true,
				searchList: [],
				value:'',
				isLoading:false
			}
		},
		computed: {
			// 解析mapstate
			...mapState([
				// 这里使用了上面data就不需要再使用了
				'historyLists' //这个是定义在store中index.js中的值
			])
		},

		methods: {
			setHistory(){
				// set_history在store/index.js中定义的
				this.$store.dispatch('set_history', {
					// 将搜索后的关键字传入页面
					name: this.value
				})
			},
			clickHistory(item){
				this.value = item.name
				// 执行搜索
				this.getSearch(this.value)
			},
			change(value) {
				//判断value是否为空
				if (value === '') {
					//清除定时器
					clearTimeout(this.timer)
					// 将标记mark设置为false
					this.mark = false
					this.getSearch(value)
					return
				}
				//这个判断是暂时限制请求次数
				if (!this.mark) {
					this.mark = true //初始为undefined 后进入了就改为true
					// 每隔一段时间就会执行一次搜索
					this.timer = setTimeout(() => {
						this.mark = false //搜索完成后将mark设置为false
						this.getSearch(value)
					}, 1500);
				}
			},
	
			getSearch(value) {
				if(!value){
					this.searchList = []
					this.isHistory = true
					return
				}
				this.isHistory = false
				// 只有加载的时候会出现
				this.isLoading = true
				// 请求所有数据
				this.$api.get_search({
					// 将收到的值传递给云函数
					value,
				}).then(res => {
					const {
						data
					} = res
					this.searchList = data
					// 搜索到数据就改为false
					this.isLoading = false
				}).catch(()=>{
					//处理错误
					this.isLoading = false
				})
			},
			//清空的方法
			clear(){
				this.$store.dispatch('clearHistory')
				uni.showToast({
					title:"清空成功"
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
		background-color: #fdfcfc;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		border: 1px re solid;

		.top {
			background: #fff;
		}

		.labelBox {
			background: #fff;
			margin-bottom: 10px;

			.labelHeader {
				display: flex;
				justify-content: space-between;
				// border: 2px solid forestgreen;
				font-size: 16px;
				color: #666;
				padding: 10px 15px;
				border-bottom: 1px #f5f5f5 solid;

				.labelTitle {
					color: $mk-base-color;
				}

				.labelClear {
					color: $mk-base-color;
					font-weight: bold;
				}
			}

			.labelCont {
				display: flex;
				// 自动换行
				flex-wrap: wrap;
				padding: 15px;
				padding-top: 0;

				.labelContItem {
					padding: 2px 10px;
					margin-top: 12px;
					margin-right: 10px;
					border: 1px #b8b8b8 solid;
					border-radius: 10px;
					font-size: 14px;
					color: #666;
				}
			}
		}

		.result {
			margin-top: 20px;

		}
	}

	.withoutData {
		height: 200px;
		line-height: 200px;
		width: 100%;
		text-align: center;
		font-size: 14px;
		color: #666;
	}
</style>
